package com.xl.rentkeeper.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xl.rentkeeper.R;
import com.xl.rentkeeper.entity.Order;

/**
 * Created by Administrator on 2015/7/21.
 */
public class DialogUtil {

    public interface DialogCallback {
        public void onConfirm(Object o);
    }

    public static Dialog showErrInfoDialog(Context context, int errMsg) {
        return showErrInfoDialog(context, context.getResources().getString(errMsg));
    }

    public static Dialog showErrInfoDialog(Context context, String errMsg) {
        AlertDialog.Builder b = new AlertDialog.Builder(context);
        final Dialog dialog = b.create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_err, null);
        RelativeLayout rlt_sure = (RelativeLayout) view.findViewById(R.id.rlt_sure);
        TextView tv_errmsg = (TextView) view.findViewById(R.id.tv_errmsg);
        tv_errmsg.setText(errMsg);
        rlt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setContentView(view);
        return dialog;
    }

    public static Dialog showProgressDialog(Context context, int msg) {
        ProgressDialog dialog = ProgressDialog.show(context, null, context.getResources().getString(msg));
        dialog.setCancelable(true);
        return dialog;
    }

    public static Dialog showBackDialog(Context context, int title, int msg, int pbstr, DialogInterface.OnClickListener pbclickLinstener, int nbstr, DialogInterface.OnClickListener nbClickLinstener) {
        AlertDialog.Builder b = new AlertDialog.Builder(context);
        if (title != 0)
            b.setTitle(title);
        if (msg != 0)
            b.setMessage(msg);
        if (pbstr != 0)
            b.setPositiveButton(pbstr, pbclickLinstener);
        if (nbstr != 0)
            b.setNegativeButton(nbstr, nbClickLinstener);
        return b.create();
    }

    public static void showTextDialog(Context context, final DialogCallback callback, final Order order) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("这个客户我来搞定!");
        builder.setTitle("确认接单");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callback != null) {
                    callback.onConfirm(order);
                }
                dialog.dismiss();
                // Main.this.finish();
            }
        });
        builder.setNegativeButton("还是算了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public static Dialog showMsgInfoDialog(Context context, final DismissClickListener listener, String msg, String title,String okName,String cancelName) {
        AlertDialog.Builder b = new AlertDialog.Builder(context);
        final Dialog dialog = b.create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog, null);

       // TextView mTVTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView mTVOk = (TextView) view.findViewById(R.id.tv_ok);
        TextView mTVCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView mTVMsg = (TextView) view.findViewById(R.id.tv_msg);

        if (okName != null){
            mTVOk.setText(okName);
        }
        if (cancelName != null){
            mTVCancel.setText(cancelName);
        }

        mTVMsg.setText(msg);
       // mTVTitle.setText(title);
        mTVOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.okClick();
                dialog.dismiss();
            }
        });
        mTVCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.cancelClick();
                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.setContentView(view);
        return dialog;
    }

    public interface DismissClickListener {
        void okClick();
        void cancelClick();
    }
}
